import { eventBus } from './eventBus';
import { AgentStatus } from '../model/agent/type';
import { IAgentService } from '../model/agent/service';
import { AgentConnectedEvent } from '../event/agentConnected';
import { AgentDisconnectedEvent } from '../event/agentDisconnected';
export class MonitorService {

  constructor(private agentService: IAgentService) { }
  init() {
    eventBus.on(AgentDisconnectedEvent.NAME, async (event: AgentDisconnectedEvent) => {
      const agentDto = await this.agentService.findByIdentity(event.identity);
      if (agentDto) {
        agentDto.status = AgentStatus.OFFLINE;
        agentDto.disconnectedAt = new Date();
        await this.agentService.save(agentDto);
      }
    });
    eventBus.on(AgentConnectedEvent.NAME, async (event: AgentConnectedEvent) => {
      const agentDto = await this.agentService.findByIdentity(event.identity);
      if (agentDto) {
        agentDto.status = AgentStatus.ONLINE;
        await this.agentService.save(agentDto);
      }
    });
  }
}